Adding Active Server Pages |
What are Active Server Pages? |
Displaying Date, Time, and Text |
Using Counters, Variables, and Forms |
Displaying Server Statistics |
Active Server Pages Server-Side Scripting Programmer's Reference |
The date and time described in this section are those that are on your computer, so before you display them in a Web page, you should make sure that the correct date and time are reflected on your system. The Displaying Text Section follows.
<% =date %>
at the point where you want it to appear. When you view the page in Microsoft Internet Explorer, you should see something like this:
Thu, Jan 23, 1997
Note: Even though "=date" is a short script, it's actually made up of two parts. The "date" part tells the server, "Get me the date." The equal sign (=) tells the server to display the date in the Web page. If you typed just:
<% date %>
the server would get the current date from your system, but that's all. It wouldn't display it. There are times when it makes sense to use an ASP function without the equal sign. Later we'll see an example that does this.
<% =time %>
where you want it to appear. When you view the page, you should see something like this:
4:19:46 PM
<% =now %>
where you want them to appear. When you view the page, you should see something like this:
1/23/97 4:19:46 PM
To display the number of the current month in a Web page, type:
<% =month(now) %>
where you want it to appear. When you view the page in Internet Explorer, you'll see a 1 if the current month is January, 2 if it's February, and so on.
To display the name of the current month, type:
<% =monthname(month(now)) %>
where you want it to appear.
To display the day of the current month, type:
<% =day(now) %>
where you want it to appear. When you view the page, you'll see a number between 1 and 31.
To display the current year, type:
<% =year(now) %>
where you want it to appear.
Suppose you wanted to display today's date as day/month/year instead of month/day/year. To do so, you would use the day, month, and year ASP functions together, by typing:
<% =day(now) %>/<% =month(now) %>/<% =year(now) %>
When you viewed the page, you would see something like this:
23/1/1997
Later we'll see how you can change this so only the last two digits of the year are displayed, like this:
23/1/97
To display the day of the week as a number from 1 to 7 in a Web page, type:
<% =weekday(now) %>
where you want it to appear. When you view the page in Internet Explorer, you'll see a 1 if today is Sunday, 2 if it's Monday, and so on.
To display the day of the week by name, type:
<% =weekdayname(weekday(now)) %>
where you want it to appear.
To display just the hour part of the current time, type:
<% =hour(now) %>
where you want it to appear. The hour function is based on a 24-hour clock. When you view the page, you'll see a number between 0 and 23.
To display just the minutes part of the current time, type:
<% =minute(now) %>
where you want it to appear. When you view the page, you'll see a number between 0 and 59.
To display just the seconds part of the current time, type:
<% =second(now) %>
where you want it to appear. When you view the page, you'll see a number between 0 and 59.
Try typing this into a Web page:
The time is <% =time %>. That means it's <% =minute(now) %> minutes past <% =hour(now) %> o'clock.
When you view the page in Internet Explorer, you should see something like this:
The time is 1:36:05 PM. That means it's 36 minutes and 5 seconds past 13 o'clock.
Remember, the hour function is based on a 24-hour clock. Later we'll see how to convert from the 24-hour clock to a 12-hour clock.
You probably won't ever use the timevalue function. It takes the different ways you can write the time, such as "2:24PM" and "14:24," and returns them in this format: "2:24:00 PM." This can be useful if you're using a function that needs to be given the time in that exact format.
Earlier in this section we saw how you can use the hour, minute, and second functions to break up the time into hours, minutes, and seconds. With the timevalue function, you can put them back together. Type this into a Web page:
When it's 23 minutes and 5 seconds past 4 o'clock in the afternoon, that means it's <% =timevalue("16:23:05") %>. This is the same as <% =timevalue("4:23:05PM") %> or <% =timevalue("16:23:05PM") %>.
Make sure you type "16:23:05PM" and not "16:23:05 PM." The "05" and the "PM." should be run together, not separated by a space. When you view the page in Internet Explorer, you should see:
When it's 23 minutes and 5 seconds past 4 o'clock in the afternoon, that means it's 4:23:05 PM. This is the same as 4:23:05 PM or 4:23:05 PM.
There are <% =len("The cat is on the mat.") %> characters in "The cat is on the mat."
When you view the page in Internet Explorer, you should see this:
There are 22 characters in "The cat is on the mat."
"Frankenstein" begins with the letter <% =left("Frankenstein", 1) %>.
When you view the page, you should see this:
"Frankenstein" begins with the letter F.
The last three letters of "Wednesday" are: <% =right("Wednesday", 3) %>.
When you view this page, you should see this:
The last three letters of "Wednesday" are: day.
You could start with the fifth character from the left and then stop at the second character from the right. Or you could do it the following way.
Try typing this into a Web page:
<% =right("pineapples", 6) %> <% =left(right("pineapples", 6), 5) %>
This line takes the last six letters of the word "pineapples," which make up the word "apples." Then it takes the first five letters of the word "apples," which make up the word "apple."
When you view this page in Internet Explorer, you should see this:
apples apple
Then try typing this into a Web page:
<% =left("pineapples", 9) %> <% =right(left("pineapples", 9), 5) %>
This line takes the first nine letters of the word "pineapples," which make up the word "pineapple." Then it takes the last five letters of the word "pineapple," which make up the word "apple."
When you view this page, you should see this:
pineapple apple
To make the link, type
<a href= <% weekdayname(weekday(now)) %>.htm>Link of the Day</a>
where you want it to appear. When you click this link in Internet Explorer, it will take you to today's page.
Earlier we saw how to change the date display from month/day/year to day/month/year like this:
23/1/1997
We can also change the date display so only the last two digits of the year are included. To do this, type
<% =day(now) %>/<% =month(now) %>/<% =(year(now) - 1900)) mod 100 %>
Now when you view the page, you should see something like this:
23/1/97
In an earlier example, we wrote a server-side script to display the current time in words, such as: "The time is 36 minutes and 5 seconds past 13 o'clock." This script used the ASP hour function, which returns just the hour part of the current time, based on a 24-hour clock.
In this example, we'll see how to change 24-hour clock times such as "13 o'clock" to 12-hour clock times ("1 o'clock PM"). To do this, we'll need to make the server-side script that uses the hour function a little more complicated. Instead of
<% =hour(now) %> o'clock
we'll need to write a script that looks at the hour and does one of the following:
The script is shown below. It isn't written quite the way a programmer would write it, but it works, and it's fairly easy to understand, since it follows the items in the bulleted list above exactly.
The hour is <% if hour(now) = 0 then %> midnight. <% end if %> <% if hour(now) = 12 then %> noon. <% end if %> <% if (hour(now) >= 1) and (hour(now) <= 11) then %> <% =hour(now) %> o'clock AM. <% end if %> <% if (hour(now) >= 13) and (hour(now) <= 23) then %> <% =hour(now) - 12 %> o'clock PM. <% end if %>
If you type (or better yet, cut-and-paste) this script in a Web page, when you view the page, you should see something like this:
The hour is 4 o'clock PM.
Suppose you wanted to put something on your Web site that only you, the owner, could see. To see it, you'd have to log in using the login name and password that are specified in the Sharing Setup control panel on your Macintosh. In fact, the site administration link on the default page that comes with Personal Web Server is set up this way. Here's how it's done.
First, you need a link to the hidden text in your default.asp file:
<A HREF="/?signin=true/">This is the link.</A>
When you view this link in Internet Explorer, it looks like a normal link. But when you click the link, instead of a new Web page, a dialog box appears asking you for your name and password. After you type the correct name and password, the hidden text appears.
The text to be hidden is enclosed in the following if-then statement, also in your default.asp file:
<% if tools.owner then %> The hidden text goes here. <% end if %>
Return to top |